package com.ssm.driver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ssm.common.utils.PageUtils;
import com.ssm.driver.dao.CashOrderDao;
import com.ssm.driver.entity.CashOrderEntity;
import com.ssm.driver.entity.SsmSjEntity;
import com.ssm.driver.entity.vo.CashOrderDto;
import com.ssm.driver.service.CashOrderService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;


@Service("cashOrderService")
@AllArgsConstructor
public class CashOrderServiceImpl extends ServiceImpl<CashOrderDao, CashOrderEntity> implements CashOrderService {
    @Resource
    public final CashOrderDao cashOrderDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<CashOrderEntity> page = this.page(
//                new Query<CashOrderEntity>().getPage(params),
//                new QueryWrapper<CashOrderEntity>()
//        );
        if (params.get("key")!=null&& StringUtils.isNotBlank(params.get("key").toString())){
            String condition = (String) params.get("key");
            IPage<CashOrderDto> page = cashOrderDao.selectJoinPage(new Page<>(Integer.parseInt(params.get("page").toString()),Integer.parseInt(params.get("limit").toString())),
                    CashOrderDto.class,
                    new MPJLambdaWrapper<CashOrderEntity>()
                            .selectAll(CashOrderEntity.class)
                            .selectAs(SsmSjEntity::getSjName,CashOrderDto::getSjName)
                            .selectAs(SsmSjEntity::getSjPhone,CashOrderDto::getSjPhone)
                            .leftJoin(SsmSjEntity.class,SsmSjEntity::getSjId,CashOrderDto::getSjId)
                            .like(SsmSjEntity::getSjName,condition).or()
                            .like(SsmSjEntity::getSjPhone,condition));
            return new PageUtils(page);
        }
        IPage<CashOrderDto> page2 = cashOrderDao.selectJoinPage(new Page<>(Integer.parseInt(params.get("page").toString()),Integer.parseInt(params.get("limit").toString())),
                CashOrderDto.class,
                new MPJLambdaWrapper<CashOrderEntity>()
                        .selectAll(CashOrderEntity.class)
                        .selectAs(SsmSjEntity::getSjName,CashOrderDto::getSjName)
                        .selectAs(SsmSjEntity::getSjPhone,CashOrderDto::getSjPhone)
                        .leftJoin(SsmSjEntity.class,SsmSjEntity::getSjId,CashOrderDto::getSjId));


        return new PageUtils(page2);
    }

}